define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'apply/carousel/index' + location.search,
                    add_url: 'apply/carousel/add',
                    edit_url: 'apply/carousel/edit',
                    del_url: 'apply/carousel/del',
                    multi_url: 'apply/carousel/multi',
                    import_url: 'apply/carousel/import',
                    table: 'carousel',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'img', title: '图片',formatter:Table.api.formatter.image},
                        {field: 'sort', title: __('Sort')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });


            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone", this)
                    .off("click")
                    .removeClass("btn-editone")
                    .addClass("btn-addtabs")
                    .prop("title", __('Edit'));

                // 预览图弹出处理
                $('img').parent('a').attr('href','javascript:;');//去掉图片oss连接地址
                $('img').bind('click',function(){
                    var imgs = $(this).parents('td').find('a img');
                    var dataJson = [],item={};
                    for (var i = 0; i < imgs.length; i++) {
                        item['alt'] = '';
                        item['pid'] = Math.random()*10;
                        item['src'] = item['thumb'] = $(imgs[i]).attr('src');
                        dataJson.push(item);
                        item = {}; // 重置
                    }
                    var json =  {
                        "title": "", //相册标题
                        "id": 123, //相册id
                        "start": 0, //初始显示的图片序号，默认0
                        "data": dataJson
                    };
                    Layer.photos({
                        photos: json,
                        anim: 0 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                    });
                })
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
